package controller

import (
	"gitee.com/molonglove/goboot/gin"
	"helper-api/app/constant"
	"helper-api/app/models/entity"
	"helper-api/app/models/request"
	"helper-api/app/models/response"
	"helper-api/app/service"
	"helper-api/utils"
	"net/http"
	"strconv"
)

var Software = new(SoftwareController)

type SoftwareController struct{}

// Page 分页查询
func (s *SoftwareController) Page(c *gin.Context) {
	var (
		page  *response.PageData
		param request.SoftwarePageRequest
		err1  *response.BusinessError
		err   error
	)
	if err = c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if page, err1 = service.Software.PageSearch(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(err1))
		return
	}
	c.JSON(http.StatusOK, response.Ok(page))
}

// Download 下载软件到指定位置
func (s *SoftwareController) Download(c *gin.Context) {
	var (
		userId int64
		err    *response.BusinessError
		id     int64
		value  string
	)
	if value = c.Query("id"); value == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	userId = c.GetInt64(constant.HeaderUserKey)
	id, _ = strconv.ParseInt(value, 10, 64)
	if err = service.Software.Download(id, userId); err != nil {
		c.JSON(http.StatusOK, response.Fail(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok("正在下载安装包"))
}

// Types 类型
func (s *SoftwareController) Types(c *gin.Context) {
	var (
		types []string
		err   *response.BusinessError
	)
	if types, err = service.Software.Types(); err != nil {
		c.JSON(http.StatusOK, response.Fail(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok(types))
}

// Create 创建软件
func (s *SoftwareController) Create(c *gin.Context) {
	var (
		param     request.SoftwareCreateRequest
		customErr *response.BusinessError
		result    *entity.Software
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Software.Create(&param); customErr != nil {
		c.JSON(http.StatusOK, response.Fail(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// Update 更新软件
func (s *SoftwareController) Update(c *gin.Context) {
	var (
		param     request.SoftwareUpdateRequest
		customErr *response.BusinessError
		result    *entity.Software
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Software.Update(&param); customErr != nil {
		c.JSON(http.StatusOK, response.Fail(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// Delete 删除软件
func (s *SoftwareController) Delete(c *gin.Context) {
	var (
		userId    int64
		param     string
		ids       []int64
		customErr *response.BusinessError
		err       error
	)
	if param = c.DefaultQuery("ids", ""); param == "" {
		c.JSON(http.StatusOK, response.Fail(response.Menus[response.RequestParamError]))
		return
	}
	if ids = utils.StrToArray(param); len(ids) <= 0 {
		c.JSON(http.StatusOK, response.Ok("暂无数据需要操作"))
		return
	}
	userId = c.GetInt64(constant.HeaderUserKey)
	if customErr = service.Software.Delete(ids, userId); customErr != nil {
		c.JSON(http.StatusOK, response.Fail(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok(response.Menus[response.DeleteSuccess]))
}

// Install 软件安装
func (s *SoftwareController) Install(c *gin.Context) {

}
